package com.hurl.waguji.biz.download;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.DateUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjJjsjbThsBO;
import com.hurl.waguji.persistence.JcsjJjsjbThsBOId;

class ProfileJjsjbThsProcessor extends BaseDownloadForUpdateProcessor {

	public ProfileJjsjbThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}
		String gpdm = context.getEnvAsString("gpdm");
		String tmp = null;

		tmp = StringUtils.substringBetween(html, "解禁股份类型</td>", "</table>");
		if(StringUtils.isNotBlank(tmp)){
			List<String> trlist = StringUtils.substringsBetween(tmp, "<tr>", "</tr>");
			for(String tr : trlist){
				JcsjJjsjbThsBO jjsjb = new JcsjJjsjbThsBO();
				List<String> tmplist = StringUtils.substringsBetween(tr, "<td", "</td>");
				//解禁日
				String jjr = tmplist.get(0);
				jjr = StringUtils.substringBetween(jjr, "<span>","</span>");
				Date jjrD = null;
				try {
					jjrD = DateUtils.parseDate(jjr, DateUtils.FORMAT_DATE);
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
				jjsjb.setId(new JcsjJjsjbThsBOId(gpdm,jjrD));
				
				//解禁股份数
				String jjgfs = tmplist.get(1);
				jjgfs = StringUtils.substringBetween(jjgfs,"<span>","</span>");
				if(jjgfs.indexOf("亿")!=-1){
					jjgfs = StringUtils.substringBetween(jjgfs, null,"亿");
					jjsjb.setJjgfs(Double.valueOf(Formatter.toDouble(jjgfs)*10000*10000).longValue());
				}else if(jjgfs.indexOf("万")!=-1){
					jjgfs = StringUtils.substringBetween(jjgfs, null,"万");
					jjsjb.setJjgfs(Double.valueOf(Formatter.toDouble(jjgfs)*10000).longValue());
				}else{
					jjsjb.setJjgfs(Double.valueOf(Formatter.toDouble(jjgfs)).longValue());
				}
				//解禁股占总股本比例
				String bl = tmplist.get(4);
				bl = StringUtils.substringBetween(bl,"<span>", "</span>");
				bl = StringUtils.substringBetween(bl, null,"%");
				jjsjb.setJjgzzgbbl(Formatter.toDouble(bl)/100);
				//解禁股份类型
				String lx = tmplist.get(5);
				lx = StringUtils.substringBetween(lx, "<span>","</span>");
				jjsjb.setJjgflx(Formatter.toString(lx));
				jjsjb.setLastupdate(currentTime);
				
				collector.addBO(jjsjb);
			}
		}
	
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/gbyj.html";
			}
		};
	}
}
